<template>
	<div>
		<el-dialog title="选择科室" :visible.sync="depaDist">
		  <div class="departSearch">
		  	<el-input v-model="keyword" placeholder="请输入内容" class="pop_input"></el-input>
		  	<el-button type="success" class="pop_btn" @click="searchFun">搜索</el-button>
		  	<el-button type="primary" class="pop_btn" @click="clearFun">清空</el-button>
		  </div>
		  <Graytable :tHead="tHead" :tData="tData" @clickFun="clickFun"></Graytable>
		  <pagination :total="query.total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name:"Distributdepart",
		data () {
			return {
				keyword:"",
				depaDist:false,
				query:{
					total:0,
					page:1,
					limit:10
				},
				tHead:[
					{
						prop:"KSDM",
						label:"科室编码"
					},
					{
						prop:"KSMC",
						label:"科室名称"
					},
					{
						prop:"select",
						label:"是否已选择",
						click:true
					},
				],
				tData: [],
				id:""
			}
		},
		props:{
			dataObj:{
				required: true,
				type:Object
			},
			type:{
				type:String,
				default:"mz"
			}
		},
		watch:{
			dataObj(val){
				this.id=this.dataObj.id;
				this.getList();
			}
		},
		methods:{
			async getList(){
				let res=await this.$ptc.pharDept({
					page:this.query.page,
					keyword:this.keyword,
					type:this.type,
					phar_id:this.id,
					pageSize:this.query.limit,
				});
				if(res.total>0){
					this.query.total=res.total;
					for(var i=0;i<res.rows.length;i++){
						res.rows[i].select= (res.rows[i].phar_id == 1)?"<span style='color:green;cursor:pointer;'>已选择</span>":"<span style='color:red;cursor:pointer;'>未选择</span>";
					}
					this.tData=res.rows;
				}
			},
			searchFun(){
				this.query={
					page:1,
					limit:10,
					total:0
				};
				this.getList();
			},
			async clearFun(){
				let res=await this.$ptc.phrClean({
					phar_id:this.id,
					type:this.type
				})
				this.$message({
				  message: res.msg,
				  type: res.code == 1?'success':'error',
				  duration:1500
				});
				if(res.code == 1){
					this.getList();
					this.$emit("distdepaChange")
				}
			},
			async clickFun(row){
				var res;
				if(row.phar_id == 1){
					res=await this.$ptc.delDeptToPhar({
						phar_id:this.id,
						dept_id:row.id,
						type: this.type
					});
				}else{
					res=await this.$ptc.addDeptToPhar({
						phar_id:this.id,
						dept_id:row.id,
						type: this.type
					});
				}
				this.$message({
				  message: res.msg,
				  type: res.code == 1?'success':'error',
				  duration:1500
				});
				if(res.code == 1){
					this.getList();
					this.$emit("distdepaChange")
				}
			},
			show(){
				this.depaDist=true;
			},
			hide(){
				this.depaDist=false;
			}
		}
	}
</script>

<style>
</style>
